package com.byedu.course;

import com.byedu.base.BaseService;
import com.byedu.domain.course.CourseCount;
import com.byedu.domain.course.vo.UserCourseVO;
import com.byedu.domain.course.UserCourseDO;
import com.byedu.mapper.course.UserCourseMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jason on 2017/5/21.
 */
@Service
public class UserCourseService extends BaseService {
    @Autowired
    private UserCourseMapper userCourseMapper;

    public Integer addUserCourse(UserCourseVO userCourseVO) {
        UserCourseDO userCourseDO = new UserCourseDO();

        Integer result = 0;
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = format1.format(new Date());
        try {
            userCourseVO.setPurchaseTime(date);
            BeanUtils.copyProperties(userCourseDO, userCourseVO);
            result = userCourseMapper.addUserCourse(userCourseDO);
        } catch (Exception e) {
            logger.info("保存用户购买课程信息出错",e);
        }

        return result;
    }

    public void addUserCourses(List<UserCourseVO> userCourseVOList) {
        List<UserCourseDO> userCourseDOList = convertVOListToDOList(userCourseVOList);
        if (userCourseDOList != null) {
            userCourseMapper.addUserCourses(userCourseDOList);
        }
    }

    public List<UserCourseDO> convertVOListToDOList(List<UserCourseVO> userCourseVOList) {
        if (CollectionUtils.isNotEmpty(userCourseVOList)) {
            List<UserCourseDO> userCourseDOList = new ArrayList<UserCourseDO>();
            for (UserCourseVO userCourseVO : userCourseVOList) {
                try {
                    UserCourseDO userCourseDO = new  UserCourseDO();
                    BeanUtils.copyProperties(userCourseDO, userCourseVO);
                    userCourseDOList.add(userCourseDO);
                } catch (Exception e) {
                    logger.info("课程信息转换错误",e);
                }
            }

            return userCourseDOList;
        }

        return null;
    }


    public List<UserCourseVO> convertDOListToVOList(List<UserCourseDO> userCourseDOList) {
        if (CollectionUtils.isNotEmpty(userCourseDOList)) {
            List<UserCourseVO> userCourseVOList = new ArrayList<UserCourseVO>();

            for (UserCourseDO userCourseDO : userCourseDOList) {
                try {
                    UserCourseVO userCourseVO = new  UserCourseVO();
                    BeanUtils.copyProperties(userCourseVO, userCourseDO);
                    userCourseVOList.add(userCourseVO);
                } catch (Exception e) {
                    logger.info("课程信息转换错误",e);
                }
            }

            return userCourseVOList;
        }

        return null;
    }



    public boolean getbayCourse(String userId, String courseId) {

        UserCourseDO userCourseDO = userCourseMapper.getbayCourse(userId, courseId);
        boolean isTrue= false;
        if (userCourseDO != null) {
            isTrue=true;
        }
        return isTrue;
    }


    public List<UserCourseVO> getAllUserCourse() {

        List<UserCourseVO> userCourseVOList = convertDOListToVOList(userCourseMapper.getbayallCourse());

        return userCourseVOList;
    }

    public List<CourseCount> getcidcount(){
        return userCourseMapper.getcidcount();
    }

    public List<CourseCount> getuidcount(){
        return userCourseMapper.getuidcount();
    }

    public List<CourseCount> getrankcount(){
        return userCourseMapper.getrankcount();
    }

}
